package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.PanelContentConverter;
import com.mall.shopping.converter.PanelConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.persistence.*;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.HomePageResquest;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Set;

/**
 * @author ShuiYi
 * @time 2021-07-22 9:41
 */
@Slf4j
@Service
public class IHomeServiceImpl implements IHomeService {

    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemCatMapper itemCatMapper;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    PanelConverter panelConverter;
    @Autowired
    PanelContentConverter panelContentConverter;

    @Override
    public HomePageResponse homepage(HomePageResquest request) {
        HomePageResponse homePageResponse = new HomePageResponse();

        try {
            //校验有效性
            request.requestCheck();
            //条件查询
            Example example = new Example(Panel.class);
            List<Panel> panels = panelMapper.selectByExample(example);
            Set<PanelDto> panelDtos = panelConverter.panelstoDtos(panels);
            //查询PanleContentItemDto
            for (PanelDto panelDto : panelDtos) {
                Example example1 = new Example(PanelContent.class);
                Example.Criteria criteria = example1.createCriteria();
                criteria.andEqualTo("panelId",panelDto.getId());
                List<PanelContent> panelContents = panelContentMapper.selectByExample(example1);


                List<PanelContentItemDto> panelContentItemDtos = panelContentConverter.panelContentToDos(panelContents);
                for (PanelContentItemDto panelContentItemDto : panelContentItemDtos) {

                    Example example2 = new Example(Item.class);
                    Example.Criteria criteria1 = example2.createCriteria();
                    criteria1.andEqualTo("id",panelContentItemDto.getProductId());
                    List<Item> items = itemMapper.selectByExample(example2);
                    panelContentItemDto.setProductName(items.get(0).getTitle());
                    panelContentItemDto.setSalePrice(items.get(0).getPrice());

                }
                panelDto.setPanelContentItems(panelContentItemDtos);
            }


            homePageResponse.setPanelContentItemDtos(panelDtos);
            homePageResponse.setCode(ShoppingRetCode.SHOPPING_HOMEPAGE.getCode());
            homePageResponse.setMsg(ShoppingRetCode.SHOPPING_HOMEPAGE.getMessage());

        }catch (Exception e) {
            log.info("IHomeServiceImpl#homepage"+e);
            homePageResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            homePageResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }

        return homePageResponse;
    }
}
